import Polygon from '@arcgis/core/geometry/Polygon';
import Polyline from '@arcgis/core/geometry/Polyline';
import Graphic from '@arcgis/core/Graphic';
import SimpleLineSymbol from '@arcgis/core/symbols/SimpleLineSymbol';
import Draw from '@arcgis/core/views/draw/Draw'
import MapView from "@arcgis/core/views/MapView"; //视图
import { SymbolUtils } from "../utils/symbol-utils";

/**
 * 地图上，画线、面 的通用操作
 */
export class DrawUtils {
    public _mapView: MapView;
    public constructor(mapView: MapView) {
        this._mapView = mapView;
    }
    /**
     * 画线
     */
    public drawpolyline() {
        var draw = new Draw({
            view: this._mapView
        });
        const action = draw.create("polygon");
        this._mapView.focus();
        action.on([
            "vertex-add",
            "vertex-remove",
            "cursor-update",
            "redo",
            "undo",
            "draw-complete",
        ], (event) => {
            console.log('this._mapView.spatialReference', this._mapView.spatialReference);
            this._mapView.graphics.removeAll();
            const vertices = event.vertices;
            const graphic = new Graphic({
                geometry: new Polyline({
                    paths: [vertices],
                    spatialReference: this._mapView.spatialReference
                }),
                symbol: new SimpleLineSymbol({
                    color: [4, 90, 141],
                    width: 4,
                    cap: "round",
                    join: "round"
                })
            })
            this._mapView.graphics.add(graphic);
        })
    }
    /**
     * 画多边形（面）
     */
    public createPolygon() {
        var draw = new Draw({
            view: this._mapView
        });
        const action = draw.create("polygon");
        this._mapView.focus();
        action.on([
            "vertex-add",
            "vertex-remove",
            "cursor-update",
            "redo",
            "undo",
            "draw-complete",
        ], (event) => {
            this._mapView.graphics.removeAll();
            const vertices = event.vertices;
            const graphic = new Graphic({
                geometry: new Polygon({
                    rings: [vertices],
                    spatialReference: this._mapView.spatialReference
                }),
                symbol: SymbolUtils.getFillSymbol()
            })
            this._mapView.graphics.add(graphic);
        })
       
    }

}
